import 'package:city_pickers/city_pickers.dart';
import 'package:du_application/data.dart';
import 'package:du_application/model/InfoModel.dart';
import 'package:du_application/provide/InfoProvider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';

class AddressEditPage extends StatefulWidget {
  Address address;
  AddressEditPage({Key key, this.address}) : super(key: key);

  @override
  _AddressEditPageState createState() => _AddressEditPageState();
}

class _AddressEditPageState extends State<AddressEditPage> {
  @override
  Widget build(BuildContext context) {
    updateShow() {
      CityPickers.showFullPageCityPicker(
        theme: ThemeData(
          accentColor: Colors.black,
        ),
        context: context,
        provincesData: provincesData,
        citiesData: citiesData,
      ).then((Result result) {
        widget.address.province = result.provinceId;
        widget.address.city = result.cityId;
       widget.address.county = result.areaId;
        widget.address.areaCode = result.areaId;
      });
    }

    return Provide<InfoProvider>(builder: (context, child, code) {
      return Scaffold(
          body: GestureDetector(
        child: Stack(children: [
          Positioned(
              right: ScreenUtil().setWidth(10),
              top: ScreenUtil().setWidth(10),
              child: IconButton(
                icon: Icon(Icons.close),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )),
          Container(
            padding: EdgeInsets.symmetric(
                horizontal: ScreenUtil().setWidth(30),
                vertical: ScreenUtil().setHeight(120)),
            height: ScreenUtil().setHeight(1800),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                TextField(
                  textInputAction: TextInputAction.done,
                  onChanged: (value) {
                    setState(() {
                      widget.address.name = value;
                    });
                  },
                  controller: TextEditingController.fromValue(TextEditingValue(
                      text:
                          '${widget.address.name == null ? "" : widget.address.name}', //判断keyword是否为空
                      selection: TextSelection.fromPosition(TextPosition(
                          affinity: TextAffinity.downstream,
                          offset: '${widget.address.name}'.length)))),

                  //  keyboardType:TextInputType.,
                  // obscureText: true,
                  style: TextStyle(color: Colors.black),
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                      //            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                      //            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
                    ),
                    labelText: '收货人',
                    labelStyle: TextStyle(color: Colors.black),
                    hintText: '请输入收货人',
                    focusColor: Colors.black,
                    focusedBorder: InputBorder.none,
                  ),
                ),
                TextField(
                  textInputAction: TextInputAction.done,
                  onChanged: (value) {
                    setState(() {
                      widget.address.tel = value;
                    });
                  },
                  keyboardType: TextInputType.number,
                  inputFormatters: [
                    WhitelistingTextInputFormatter.digitsOnly
                  ], //只允许输入数字
                  // obscureText: true,
                  style: TextStyle(color: Colors.black),
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                      //            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                      //            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
                    ),
                    labelText: '收货号码',
                    labelStyle: TextStyle(color: Colors.black),
                    hintText: '请输入收货号码',
                    focusColor: Colors.black,
                    focusedBorder: InputBorder.none,
                  ),
                  controller: TextEditingController.fromValue(TextEditingValue(
                      text:
                          '${widget.address.tel == null ? "" : widget.address.tel}', //判断keyword是否为空
                      selection: TextSelection.fromPosition(TextPosition(
                          affinity: TextAffinity.downstream,
                          offset: '${widget.address.tel}'.length)))),
                ),
                InkWell(
                    onTap: () {
                      updateShow();
                    },
                    child: Container(
                        padding: EdgeInsets.symmetric(
                            vertical: ScreenUtil().setHeight(10)),
                        decoration: BoxDecoration(
                          border: Border.all(width: ScreenUtil().setSp(2)),
                          borderRadius: BorderRadius.circular(15.0),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Container(
                              alignment: Alignment.center,
                              padding: EdgeInsets.symmetric(
                                  horizontal: ScreenUtil().setWidth(40),
                                  vertical: ScreenUtil().setWidth(20)),
                              color: Colors.cyan[400],
                              width: ScreenUtil().setWidth(200),
                              child: Text(
                                province_list[widget.address.province],
                                style: TextStyle(color: Colors.white),
                              ),
                            ),
                            Container(
                              alignment: Alignment.center,
                              padding: EdgeInsets.symmetric(
                                  horizontal: ScreenUtil().setWidth(40),
                                  vertical: ScreenUtil().setWidth(20)),
                              color: Colors.cyan[400],
                              width: ScreenUtil().setWidth(200),
                              child: Text(
                                city_list[widget.address.city],
                                style: TextStyle(color: Colors.white),
                              ),
                            ),
                            Container(
                              alignment: Alignment.center,
                              padding: EdgeInsets.symmetric(
                                  horizontal: ScreenUtil().setWidth(40),
                                  vertical: ScreenUtil().setWidth(20)),
                              color: Colors.cyan[400],
                              width: ScreenUtil().setWidth(200),
                              child: Text(
                                county_list[widget.address.county],
                                style: TextStyle(color: Colors.white),
                              ),
                            ),
                          ],
                        ))),
                TextField(
                  textInputAction: TextInputAction.done,
                  onChanged: (value) {
                    setState(() {
                      widget.address.addressDetail = value;
                    });
                  },
                  style: TextStyle(color: Colors.black),
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                      //            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                      //            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
                    ),
                    labelText: '详细地址',
                    labelStyle: TextStyle(color: Colors.black),
                    hintText: '详细地址',
                    focusColor: Colors.black,
                    focusedBorder: InputBorder.none,
                  ),
                  controller: TextEditingController.fromValue(TextEditingValue(
                      text:
                          '${widget.address.addressDetail == null ? "" : widget.address.addressDetail}', //判断keyword是否为空
                      selection: TextSelection.fromPosition(TextPosition(
                          affinity: TextAffinity.downstream,
                          offset: '${widget.address.addressDetail}'.length)))),
                ),
                Row(children: [
                  Text("设置为默认地址"),
                  Checkbox(
                    value: widget.address.isDefault, //是否选中 true选中  false不选中
                    onChanged: (value) {
                      setState(() {
                        widget.address.isDefault = value;
                      });
                    },
                    // tristate:
                    //     true, //默认false，但当为true时表示增加中间状态值，onchange的value：true 选中，null中间状态，false 不选中。
                    activeColor: Colors.cyan[400], //框的背景色，点击的效果色
                    checkColor: Colors.white, // 中间的对勾颜色，中间状态的横杠颜色
                  )
                ]),
              ],
            ),
          ),
          Positioned(
              right: ScreenUtil().setWidth(40),
              bottom: ScreenUtil().setHeight(40),
              child: InkWell(
                  onTap: () {
                    code.updateAddress(widget.address);
                    Navigator.pop(context);
                  },
                  child: Container(
                      padding: EdgeInsets.symmetric(
                          horizontal: ScreenUtil().setSp(40),
                          vertical: ScreenUtil().setSp(20)),
                      color: Colors.cyan[400],
                      margin: EdgeInsets.only(left: ScreenUtil().setSp(60)),
                      child: Text(
                        "确认",
                        style: TextStyle(color: Colors.white),
                      ))))
        ]),
        onTap: () {
          return false;
        },
      ));
    });
  }
}
